const app = getApp()
import moment from "../../libs/moment.js"
const regeneratorRuntime = require('../../libs/runtime')
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
    data: {
        background: [
            'cloud://hotel-ly303.686f-hotel-ly303/img/微信图片_20190617210403.jpg',
            'cloud://hotel-ly303.686f-hotel-ly303/img/微信图片_20190617210416.jpg',
            'cloud://hotel-ly303.686f-hotel-ly303/img/微信图片_20190617210420.jpg'
        ],
        tabs: ["酒店预订", "门店介绍"], //"饮食介绍"
        roomUnitList: [{}],
        roomUnitCollection: [],
        aboutUs: {},
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        startDate: "",
        endDate: "",
        name: 'name1',
        collapseImg: "",
        calendarConfig: {
            disablePastDay: true, // 是否禁选过去日期
            onlyShowCurrentMonth: true,
            noDefault: true
        },
        isShowStartCalendar: false,
        isShowEndCalendar: false
    },
    async onLoad(options) {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
        const db = wx.cloud.database()
        await db.collection('room_unit_list').get({
            success(res) {
                that.setData({
                    roomUnitList: res.data,
                    roomUnitCollection: res.data
                })
            },
            fail(err) {
                console.log(err)
            }
        })
        await this.getCollepseImg();
        await this.getAboutUs();
        wx.hideLoading()
    },
    tabClick: function(e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    filterRoomUnit(startDate) {
        let roomUnitList = this.data.roomUnitCollection.filter(item => {
            if (item.isRoomTypeFull) {
                return
            }
            let itemEndDateList = JSON.parse(item.roomOrderDateList)
            for (let i = 0; i < itemEndDateList.length; i++) {
                if (itemEndDateList[i] == 'null' || itemEndDateList[i] == null || moment(startDate).isSame(itemEndDateList[i].endDate) || moment(startDate).isAfter(itemEndDateList[i].endDate)) {
                    return item
                }
            }
        })
        let b = this.isOffDay(startDate)
        if (b) {
            roomUnitList.forEach(item => {
                item.currentPrice = +item.currentPrice + 100
            })
        }
        if (JSON.stringify(roomUnitList) === '{}' || !roomUnitList) {
            wx.showToast({
                title: '暂无可用房间',
                icon: "none"
            })
        } else {
            wx.showToast({
                title: '已筛选出可用房型',
            })
        }
        this.setData({
            roomUnitList: roomUnitList
        })
    },
    checkIsLegalDate(startDate, endDate) {
        if (moment(endDate).isAfter(startDate)) {
            return true
        } else {
            return false
        }
    },
    selectStartDate(startDate) {
        this.setData({
            startDate: startDate
        })
        this.filterRoomUnit(startDate)
    },
    selectEndDate(endDate) {
        let startDate = this.data.startDate;
        if (!startDate) {
            wx.showToast({
                title: '请先选择入住时间',
                icon: 'none'
            })
            return
        }
        if (this.checkIsLegalDate(startDate, endDate)) {
            this.setData({
                endDate: endDate
            })
        } else {
            wx.showToast({
                title: '离店时间需要大于入住时间',
                icon: 'none'
            })
        }
        console.log(startDate, endDate)
    },
    toRoomDetail(e) {
        let item = e.currentTarget.dataset.item
        if (this.data.startDate && this.data.endDate) {
            wx.navigateTo({
                url: `/pages/room/roomDetail?roomId=${item._id}&startDate=${this.data.startDate}&endDate=${this.data.endDate}`,
            })
        } else {
            wx.showToast({
                title: '请先选择入住日期和离店日期',
                icon: 'none'
            })
        }
    },
    async getCollepseImg() {
        const db = wx.cloud.database()
        let {
            data
        } = await db.collection('collapse_img').get()
        this.setData({
            collapseImg: data
        })
    },
    async getAboutUs() {
        const db = wx.cloud.database()
        let {
            data
        } = await db.collection('about').doc('88759eea-060a-4068-8a32-f12bf5e60bfe').get()
        this.setData({
            aboutUs: data
        })
    },
    handleImagePreview(e) {
        const idx = e.target.dataset.idx
        const images = this.data.collapseImg[0].img
        wx.previewImage({
            current: images[idx], //当前预览的图片
            urls: images, //所有要预览的图片
        })
    },
    //判断是否为周末
    isOffDay(date) {
        let d = new Date(date)
        let day = d.getDay()
        if (day >= 5 && day <= 6) {
            return true
        }
        return false
    },
    startTapDay(e) {
        let detail = e.detail;
        let date = moment([detail.year, detail.month - 1, detail.day]).format("YYYY-MM-DD")
        this.setData({
            isShowStartCalendar: false
        })
        this.selectStartDate(date)
    },
    endTapDay(e) {
        let detail = e.detail;
        let date = moment([detail.year, detail.month - 1, detail.day]).format("YYYY-MM-DD")
        this.setData({
            isShowEndCalendar: false
        })
        this.selectEndDate(date)
    },
    showCalendar(e) {
        let tmp = e.currentTarget.dataset.type
        this.setData({
            [tmp]: true
        })
    }
})